﻿using System;
using CoreTypes;
using System.Linq;
using System.Custom;
using System.Drawing;
using System.Custom.Window;
using System.Globalization;
using System.Windows.Forms;
using System.ComponentModel;
using MainModule.Properties;

namespace MainModule
{
   public partial class AccountWizard : Window
   {
      IRegisterWidget FRegisterWidget;
      CommunicationServiceObjectBase FServiceDriver;
      InternetCommunicator FCommunicator = InternetCommunicator.Self;

      public AccountWizard()
      {
         InitializeComponent();

         ColorTheme = FCommunicator.ColorTheme;

         FCommunicator.PropertyChanged += communicator_PropertyChanged;
      }
      protected override void Dispose(bool disposing)
      {
         FCommunicator.PropertyChanged -= communicator_PropertyChanged;
         
         if (disposing && (components != null))
         {
            components.Dispose();
         }
         base.Dispose(disposing);
      }

      public override ColorTheme ColorTheme
      {
         get
         {
            return base.ColorTheme;
         }
         set
         {
            base.ColorTheme = value;
            Refresh();
         }
      }

      public void RemoveAccount(Account account)
      {
         Text = Resources.S_Remove_account;
         Icon = Resources.RecycleBin;

         if (FRegisterWidget != null)
         {
            (FRegisterWidget as UserControl).Dispose();
            FRegisterWidget = null;
         }
         buttonNext.Text = Resources.S_Remove;
         buttonNext.Enabled = true;
         buttonBack.Visible = false;
         comboBoxAccounts.Visible = true;
         labelRemoveComment.Visible = true;
         labelRemoveResult.Visible = false;
         comboBoxAccounts.Items.Clear();
         comboBoxAccounts.Items.AddRange(FCommunicator.Accounts.ToArray());
         comboBoxAccounts.SelectedItem = account;
         Visualize();
      }
      public void StartRegistration(ServiceConfig config, bool modal)
      {
         Text = Resources.S_Register_new_account;
         Icon = System.Drawing.Icon.FromHandle(Resources.New16.GetHicon());

         if (FRegisterWidget != null)
         {
            (FRegisterWidget as UserControl).Dispose();
         }
         FServiceDriver = config.Service.Driver;
         FRegisterWidget = FServiceDriver.CreateRegisterWidget(config);
         FRegisterWidget.Started += new EventHandler(registerWidget_Started);
         FRegisterWidget.Faulted += new EventHandler(registerWidget_Faulted);
         FRegisterWidget.Finished += new EventHandler(registerWidget_Finished);
         FRegisterWidget.Progress += new EventHandler(registerWidget_Progress);
         FRegisterWidget.NextAllowed += new EventHandler(registerWidget_NextAllowed);
         FRegisterWidget.NextBlocked += new EventHandler(registerWidget_NextBlocked);
         panelWidgetPlace.Controls.Add((Control)FRegisterWidget);
         ((Control)FRegisterWidget).BringToFront();
         FRegisterWidget.Start();

         if (modal) ShowDialog(); else Show();
      }

      private void window_KeyDown(object sender, KeyEventArgs e)
      {
         if (e.KeyCode == Keys.Escape)
         {
            buttonCancel_Click(sender, null);
         }
      }
      private void buttonNext_Click(object sender, EventArgs e)
      {
         if (FRegisterWidget != null)
         {
            if (!FRegisterWidget.IsFinished)
            {
               if (FRegisterWidget.Next()) buttonBack.Enabled = true;
            }
            else
            {
               Close();
            }
         }
         else if (comboBoxAccounts.SelectedItem is Account)
         {
            if (MessageBox.Show(Resources.S_Do_you_really_want_to_remove_this_account, Text, MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
               Account account = comboBoxAccounts.SelectedItem as Account;

               if (FCommunicator.RemoveAccount(account))
               {
                  comboBoxAccounts.Visible = false;
                  labelRemoveResult.Visible = true;
                  labelRemoveComment.Visible = false;
                  buttonNext.Text = Resources.S_Finish;
                  comboBoxAccounts.Items.Remove(account);
                  comboBoxAccounts.SelectedItem = null;
                  labelRemoveResult.Text = Resources.S_Your_account_has_been_removed.Replace("<account>", account.ToString());
               }
               else
               {
                  FCommunicator.ShowMessage(Resources.S_Account_can_not_be_removed.Replace("<account>", account.ToString()), false);
               }
            }
         }
         else
         {
            Close();
         }
      }
      private void buttonBack_Click(object sender, EventArgs e)
      {
         FRegisterWidget.Back();
      }
      private void buttonCancel_Click(object sender, EventArgs e)
      {
         DialogResult = DialogResult.Cancel;
         Close();
      }
      private void registerWidget_Started(object sender, EventArgs e)
      {
         buttonNext.Text = Resources.S_Next;
         buttonNext.Enabled = true;
         buttonBack.Visible = true;
         buttonBack.Enabled = false;
         buttonCancel.Visible = true;
      }
      private void registerWidget_Faulted(object sender, EventArgs e)
      {
         buttonBack.Visible = true;
         buttonBack.Enabled = true;
         buttonNext.Enabled = false;
         buttonCancel.Visible = true;
      }
      private void registerWidget_Finished(object sender, EventArgs e)
      {
         buttonNext.Text = Resources.S_Finish;
         buttonNext.Enabled = true;
         buttonBack.Visible = false;
         buttonCancel.Visible = false;
      }
      private void registerWidget_Progress(object sender, EventArgs e)
      {
         buttonNext.Enabled = false;
         buttonBack.Enabled = false;
      }
      private void registerWidget_Removing(object sender, EventArgs e)
      {
         buttonNext.Text = Resources.S_Continue;
         buttonNext.Enabled = true;
         buttonBack.Visible = false;
         buttonCancel.Visible = true;
      }
      private void registerWidget_NextBlocked(object sender, EventArgs e)
      {
         buttonNext.Enabled = false;
      }
      private void registerWidget_NextAllowed(object sender, EventArgs e)
      {
         buttonNext.Enabled = true;
      }
      private void panelMain_Paint(object sender, PaintEventArgs e)
      {
         if (!AeroGlassVisible) e.Graphics.FillRectangle(Brushes.White, e.ClipRectangle);
      }
      private void panelBottom_Paint(object sender, PaintEventArgs e)
      {
         FCommunicator.DrawTheme(FCommunicator.ColorTheme, e.Graphics, (sender as Control).ClientRectangle, ColorThemeObject.ToolStrip);
      }
      private void panelWidgetPlace_Paint(object sender, PaintEventArgs e)
      {
         FCommunicator.DrawTheme(FCommunicator.ColorTheme, e.Graphics, (sender as Control).ClientRectangle, ColorThemeObject.ToolStrip);
      }
      private void communicator_PropertyChanged(object sender, PropertyChangedEventArgs e)
      {
         if (e.PropertyName == "ColorTheme") ColorTheme = FCommunicator.ColorTheme;
      }
   }
}
